package com.last;

import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.util.Scanner;

@Component
@ServerEndpoint("/websocket") //@reqeustmapping
public class WebsocketServer {


    //与客户端建立连接后的成功回调方法
    @OnOpen
    public void onOpen(Session session){

        System.out.println("与client成功建立websocket连接");

    }

    //当客户端发送message，回调该方法
    @OnMessage
    public void onMessage(String message,Session session) throws Exception{
        System.out.println(message);
        Scanner sc = new Scanner(System.in);
        String msg = sc.nextLine();
        //给客户端回话
        session.getBasicRemote().sendText(msg);
    }

    @OnClose
    public void onClose(){
        System.out.println("与客户端关闭了连接 ");
    }


}